package com.syni.mdd.sjb.community.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxCompanyDepart;
import com.syni.mdd.sjb.common.component.mybatis.community.mapper.SjbQywxCompanyDepartMapper;
import com.syni.mdd.sjb.community.base.entity.bo.DepartmentListBo;
import com.syni.mdd.sjb.community.base.service.SjbQywxCompanyDepartService;
import com.syni.mdd.sjb.community.base.utils.QxwxThirdApiUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业部门表 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2021-02-25
 */
@Service
public class SjbQywxCompanyDepartServiceImpl extends ServiceImpl<SjbQywxCompanyDepartMapper, SjbQywxCompanyDepart> implements SjbQywxCompanyDepartService {
    @Autowired
    SjbQywxCompanyDepartMapper sjbQywxCompanyDepartMapper;

    @Autowired
    QxwxThirdApiUtils qxwxThirdApiUtils;

    @Override
    public List<SjbQywxCompanyDepart> getCorpDepartData(String corpId,String accessToken) {
        List<SjbQywxCompanyDepart> sjbQywxCompanyDepartList = new ArrayList<>();
        DepartmentListBo departmentListBo = qxwxThirdApiUtils.departmentList(accessToken);
        if(departmentListBo.getErrcode() != null && !departmentListBo.getErrcode().equals("0")){
            //定位到某个页面
            return sjbQywxCompanyDepartList;
        }
        if(CollectionUtils.isNotEmpty(departmentListBo.getDepartment())){
            //查询已存在部门
            Map<Integer,SjbQywxCompanyDepart> sjbQywxCompanyDepartMap = this.getMapCorpExistDepart(corpId);
            for(DepartmentListBo.DepartmentBo departmentBo : departmentListBo.getDepartment()){
                if(!sjbQywxCompanyDepartMap.containsKey(departmentBo.getId())){
                    SjbQywxCompanyDepart sjbQywxCompanyDepart = new SjbQywxCompanyDepart();
                    sjbQywxCompanyDepart.setDepartId(departmentBo.getId());
                    sjbQywxCompanyDepart.setCorpId(corpId);
                    sjbQywxCompanyDepartMap.put(departmentBo.getId(),sjbQywxCompanyDepart);
                }
                SjbQywxCompanyDepart sjbQywxCompanyDepart = sjbQywxCompanyDepartMap.get(departmentBo.getId());
                sjbQywxCompanyDepart.setDepartName(departmentBo.getName());
            }
            if(sjbQywxCompanyDepartMap.size() > 0){
                for(Map.Entry<Integer,SjbQywxCompanyDepart> entry : sjbQywxCompanyDepartMap.entrySet()){
                    sjbQywxCompanyDepartList.add(entry.getValue());
                }
                if(CollectionUtils.isNotEmpty(sjbQywxCompanyDepartList)){
                    this.saveOrUpdateBatch(sjbQywxCompanyDepartList);
                }
            }
        }
        return sjbQywxCompanyDepartList;
    }

    public Map<Integer,SjbQywxCompanyDepart> getMapCorpExistDepart(String corpId){
        Map<Integer,SjbQywxCompanyDepart> sjbQywxCompanyDepartMap = new HashMap<>();
        List<SjbQywxCompanyDepart> sjbQywxCompanyDeparts = getCorpExistDepart(corpId);
        if(CollectionUtils.isNotEmpty(sjbQywxCompanyDeparts)){
            for(SjbQywxCompanyDepart sjbQywxCompanyDepart : sjbQywxCompanyDeparts){
                sjbQywxCompanyDepartMap.put(sjbQywxCompanyDepart.getDepartId(),sjbQywxCompanyDepart);
            }
        }
        return sjbQywxCompanyDepartMap;
    }

    public List<SjbQywxCompanyDepart> getCorpExistDepart(String corpId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(SjbQywxCompanyDepart.CORP_ID,corpId);
        List<SjbQywxCompanyDepart> sjbQywxCompanyDeparts = sjbQywxCompanyDepartMapper.selectList(queryWrapper);
        return sjbQywxCompanyDeparts;
    }


}
